package com.mazaiting.classfile.attr.info;

import com.mazaiting.classfile.ClassReader;
import com.mazaiting.classfile.attr.entity.LineNumberTableEntity;
import com.mazaiting.classfile.constant.ConstantPool;

import java.util.Arrays;

/**
 * 存放方法的行号信息
 *
 * @author mazaiting
 * @since 2021/8/12
 */
public class LineNumberTableAttribute extends AttributeInfo {
    /**
     * 行号数组长度 无符号 u2
     */
    private int length;
    /**
     * 行号数组
     */
    private LineNumberTableEntity[] lineNumberTableEntities;

    public LineNumberTableAttribute(ConstantPool pool, int index, int length) {
        super(pool, index, length);
    }

    @Override
    public void readInfo(ClassReader reader) {
        // 读取长度
        this.length = reader.readU2ToUnsignedShort();
        this.lineNumberTableEntities = new LineNumberTableEntity[length];
        for (int i = 0; i < length; i++) {
            this.lineNumberTableEntities[i] = new LineNumberTableEntity(
                    reader.readU2ToUnsignedShort(),
                    reader.readU2ToUnsignedShort()
            );
        }
    }

    @Override
    public String toString() {
        return "LineNumberTableAttribute{" +
                "length=" + length +
                ", lineNumberTableEntities=" + Arrays.toString(lineNumberTableEntities) +
                '}';
    }
}


















